const router = require('koa-router')();
const koaBody = require('koa-body')
const fs = require('fs')
const path = require('path')

router.post('/crm/product/importProduct', async (ctx, next) => {
  console.log("上传成功1",ctx.request)
  console.log(ctx.request.files)
  const file = ctx.request.files.file; // 上传的文件在ctx.request.files.file
  // 创建可读流
  const reader = fs.createReadStream(file.path);
  // 修改文件的名称
  var myDate = new Date();
  var newFilename = myDate.getTime()+'.'+file.name.split('.')[1];
  var targetPath = path.join(__dirname, '../static/upload/') + `/${newFilename}`;
  //创建可写流
  const upStream = fs.createWriteStream(targetPath);
  // 可读流通过管道写入可写流
  reader.pipe(upStream);
  //返回保存的路径
  return ctx.body = { code: 200, data: { url: 'http://' + ctx.headers.host + '/static/upload/' + newFilename } };
});


module.exports = router
